/*
  Name:        glFPSViewer.h
  Copyright:   Version 0.1
  Author:      Rodrigo Luis de Souza da Silva
  Date:        08/10/2007
  Description: Compute and show FPS of an graphic application
*/

#include "glFPSViewer.h"
#include <stdio.h>
#include <string.h>
#include <GL/glut.h>

// Empty constructor
glFPSViewer::glFPSViewer()
{
	this->setSuffix("");
	this->setPrefix("");	
}

// Constructor setting suffix and prefix
glFPSViewer::glFPSViewer(char *suf, char *pre)
{
	this->setSuffix(suf);
	this->setPrefix(pre);
}

// Setting Suffix
void glFPSViewer::setSuffix(char *suf)
{
	strcpy(this->suffix, suf);
}

// Setting Prefix
void glFPSViewer::setPrefix(char *pre)
{
	strcpy(this->prefix, pre);
}

// Compute and show FPS 
void glFPSViewer::drawFPS()
{
  char aux[16];
  static char fpsBuf[256] = {0};
  static int frames = 0;
  static int t0 = 0;
  frames++;
  GLint t = glutGet(GLUT_ELAPSED_TIME);
  if (t - t0 >= 1000) {
    GLfloat seconds = (t - t0) / 1000.0;
    GLfloat fps = frames / seconds;
    sprintf(aux, "FPS: %.1f", fps); 
    
    strcpy(fpsBuf, this->suffix);
    strcat(fpsBuf, aux);    
    strcat(fpsBuf, this->prefix);        
    
    t0 = t;
    frames = 0;
  }
  glutSetWindowTitle(fpsBuf);
}
